//
//  CollectViewController.m
//  Menu
//
//  Created by YouLu on 15/7/7.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "CollectViewController.h"

@interface CollectViewController ()

@end

@implementation CollectViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    [self createCollectArray];
    _isPush = YES;
    if (self.collectArray.count == 0){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"亲!" message:@"您还没有收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake(160*WIDTH, 120*HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(10*HEIGHT, 15*WIDTH, 10*HEIGHT, 15*WIDTH);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, MyWidth, MyHeight-64-49) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[CollectCollectionViewCell class] forCellWithReuseIdentifier:@"resuse"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    UIImageView *bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"collect.png"]];
    self.collectionView.backgroundView = bgImgView;
    [bgImgView release];
    [_collectionView release];
    [flowLayout release];
    
//-------------------编辑按钮---------------------
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    


    
    

}
//---------------自定义cell-----------------------------
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (_isDelete == YES) {
        [self createCollectArray];
        _isDelete = NO;
    }
    return self.collectArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"resuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.collectModel = [self.collectArray objectAtIndex:indexPath.row];
    
    
    return cell;
    
}

//-----------------点击进入下一页-----------------------------
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollectModel *model = [self.collectArray objectAtIndex:indexPath.row];
    if (_isPush == YES) {
        CookingViewController *cookingVC = [[CookingViewController alloc] init];
        CollectModel *collectModel = [self.collectArray objectAtIndex:indexPath.row];
        cookingVC.isCollect = YES;
        cookingVC.imagePathThumbnails = collectModel.picUrl;
        cookingVC.vegetable_id = collectModel.typeID;
        cookingVC.foodName = collectModel.foodName;
        [self.navigationController pushViewController:cookingVC animated:YES];
    }else{
        
        if (model.isEditing == NO) {
            model.isEditing = YES;
        }else{
            model.isEditing = NO;
        }
        [self.collectionView reloadData];
    }

}
-(void)rightBarAction
{
    self.tabBarController.tabBar.hidden = YES;
    _chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, MyHeight - 49, MyWidth, 49)];
    _chooseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_chooseView];
    [_chooseView release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, WIDTH*50, 44);
    [leftButton setTitle:@"删除" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    leftButton.tintColor = [UIColor blackColor];
    [_chooseView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.frame = CGRectMake(MyWidth - WIDTH*50, 0, WIDTH*50, 44);
    [rightButton setTitle:@"取消" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    rightButton.tintColor = [UIColor blackColor];
    [_chooseView addSubview:rightButton];
//-----------------------------------------------------------
    //判断能否push
    self.isPush = NO;
    for (CollectModel *model in self.collectArray) {
        model.isSelected = YES;
    }
    [self.collectionView reloadData];

}
-(void)leftBarAction
{
    self.tabBarController.tabBar.hidden = NO;
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)leftButtonAction
{
    for (CollectModel *model in self.collectArray) {
        if (model.isEditing == YES) {
            [[DataHandle shareData] deleteCollectTable:model.foodName];
        }
    }
    _isDelete = YES;
    _isPush = YES;
    [self.collectionView reloadData];
    self.tabBarController.tabBar.hidden = NO;

}
-(void)rightButtonAction
{
    for (CollectModel *model in self.collectArray) {
        model.isEditing = NO;
        model.isSelected = NO;
    }
    _isPush = YES;
    [self.collectionView reloadData];
    self.tabBarController.tabBar.hidden = NO;
}
-(void)createCollectArray
{
    [[DataHandle shareData] openDataBase];
    [[DataHandle shareData] createCollectInfoTable];
    NSMutableArray *array = [[DataHandle shareData] selectCollectInfoTabel];
    self.collectArray = [NSMutableArray arrayWithArray:array];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
